package com.lgt_enterprise_android.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.MyBaseAdapter;
import com.util.AppUtil;


public class TestAdapter extends MyBaseAdapter {

    boolean[][] grid_data;

    int mLastX, mLastY;

    private int density;

    public TestAdapter(Context context, JSONArray jsonArray) {
        super(context, jsonArray);

        density = (int)AppUtil.getdensityDpi(mContext);

        grid_data = new boolean[jsonArray.size()][Constant.Grid_column_count];

        for (int row = 0; row < jsonArray.size(); row++) {
            for (int column = 0; column < Constant.Grid_column_count; column++) {
                grid_data[row][column] = false;
            }

        }
    }

    @Override
    public View getView(final int pos, View contentView, ViewGroup viewGroup) {

        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(AppUtil.getdensityDpi(mContext) * Constant.Grid_dp_width), (int)AppUtil.getdensityDpi(mContext) * 155);
        lp.leftMargin = (int) AppUtil.getdensityDpi(mContext);


        for (int i = 0; i < Constant.Grid_column_count; i++) {
            TextView viewGrid = new TextView(mContext);
            viewGrid.setText(pos + "-" + i);

            if (grid_data[pos][i]) {
                viewGrid.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
            } else {
                viewGrid.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
            }


            linearLayout.addView(viewGrid, lp);
        }

        linearLayout.setOnTouchListener(new View.OnTouchListener() {

            int lastGridIndex = -1;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                int x = (int) motionEvent.getX();
                int y = (int) motionEvent.getY();

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastGridIndex = -1;
                        mLastX = x;
                        mLastY = y;


                    case MotionEvent.ACTION_MOVE:
                        int xDiff = Math.abs(x - mLastX);
                        int yDiff = Math.abs(y - mLastY);

                        if (xDiff < yDiff) {
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            return false;
                        } else {
                            view.getParent().requestDisallowInterceptTouchEvent(true);
                            if (xDiff >= 3 * AppUtil.getdensityDpi(mContext)) {
                                int gridWidthPx = (Constant.Grid_dp_width + 1) * density;
                                int gridIndex = (int) (x / gridWidthPx);
                                if (lastGridIndex != gridIndex) {
                                    grid_data[pos][gridIndex] = !grid_data[pos][gridIndex];
                                    View view_grid = ((ViewGroup) view).getChildAt(gridIndex);
                                    if (grid_data[pos][gridIndex]) {
                                        view_grid.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                                    } else {
                                        view_grid.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
                                    }
                                }
                                lastGridIndex = gridIndex;
                            }
                        }
                        mLastX = x;
                        mLastY = y;
                        return true;
                }
                return true;
            }
        });

        return linearLayout;
    }
}
